
package uestc.bitman.edxapp.player;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author hackeris
 * @category JavaScript interface of webview which enable the js code in webview
 *           can call the android native code.Every function exported to
 *           JavaScript interface must has a @JavascriptInterface
 */
public class PlayerJavaScriptExtra {

    private Context mContext;

    public PlayerJavaScriptExtra(Context cxt) {
        mContext = cxt;
    }

    @JavascriptInterface
    public void logd(String tag, String msg) {
        Log.d(tag, msg);
    }

    @JavascriptInterface
    public void ToastOut(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public String getVideoUrl() {
        return ((WebViewPlayerActivity) mContext).getVideoUrl();
    }

    @JavascriptInterface
    public String getTrackUrl() {
        return ((WebViewPlayerActivity) mContext).getVideoUrl().replace(".mp4", ".srt");
    }
    
    @JavascriptInterface
    public String getTrackString() {
        return ((WebViewPlayerActivity) mContext).getTrackString();
    }

    /**
     * @param url Url of get request
     * @return The return String of request
     * @category Send a get request with url
     */
    @JavascriptInterface
    public String sendGetRequest(String url) {
        HttpGet httpRequest = new HttpGet(url);
        try {
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String strResult = new String(EntityUtils.toString(httpResponse.getEntity())
                        .getBytes("ISO-8859-1"), "UTF-8");
                return strResult;
            } else {
                return httpResponse.getStatusLine().toString();
            }
        } catch (ClientProtocolException e) {
            return e.getMessage().toString();
        } catch (IOException e) {
            return e.getMessage().toString();
        } catch (Exception e) {
            return e.getMessage().toString();
        }
    }

    @JavascriptInterface
    public String urlDownloadToString(String fileUrl) {
        BufferedReader buffer;
        StringBuffer sb = new StringBuffer();
        try {
            String line = "";
            URL url = new URL(fileUrl);
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            while ((line = buffer.readLine()) != null) {
                sb.append(line + "|||");
            }
            return sb.toString();
        } catch (Exception e) {
            return e.getMessage().toString();
        }
    }

    public String readTrackFromFile(String fullPath) {
        File file = new File(fullPath);
        if (file.isDirectory()) {
            return "";
        }
        try {
            InputStream is = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "|||");
            }
            is.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
